<?php

class PageContentController extends GxController {

//    public function filters() {
//        return array('accessControl');
//    }
//
//    public function accessRules() {
//        return array(
//            array('allow', // allow admin user to perform 'admin' and 'delete' actions
//                'actions' => array('create', 'update', 'delete', 'view', 'admin', 'deleteimage'),
//                'users' => array('@'),
//            ),
//            array('deny', // deny all users
//                'users' => array('*'),
//            ),
//        );
//    }

   // public $layout = 'innerAdmin';

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'PageContent'),
        ));
    }

    public function actiondeleteimage($id) {
        $model = $this->loadModel($id, 'PageContent');
        $model->photo_name = '';
        $model->save(FALSE);

        $this->redirect(array('update', 'id' => $model->id));
    }

    public function savePageContent($model) {
        $uploadDir = YiiBase::getPathOfAlias('webroot') . '' . ECSconfig::getUploadDir() . 'cms-images/';
        $db_logo = '';
        $search = array(' ', '&', '/');
        $replace = array('-', 'and', '-');
        if (isset($_POST['PageContent'])) {
            $db_logo = $model->photo_name;
            $model->setAttributes($_POST['PageContent'], false);

            if (!empty($_FILES['PageContent']['name']['photo_name'])) {
                $model->photo_name = CUploadedFile::getInstance($model, 'photo_name');
                $fileInfo = pathinfo($model->photo_name);
                $ext = $fileInfo['extension'];
                $image = $_FILES['PageContent']['tmp_name'];
                $mainTempImages = $image['photo_name'];
                $rfilename = guid() . "." . strtolower($ext);
                $uploadnewDir = $uploadDir . $rfilename;
                $model->photo_name->saveAs($uploadnewDir);
                $db_logo = $rfilename;
            }
            $model->setAttribute('photo_name', $db_logo);
            $model->setAttribute('page_seo_title', trim(strtolower(str_replace($search, $replace, $_POST['PageContent']['page_title']))));
            $validate = $model->validate();
            if ($validate) {

                $transaction = Yii::app()->getDb()->beginTransaction();
                try {
                    if ($model->save(false)) {
                        $transaction->commit();
                        if (Yii::app()->getRequest()->getIsAjaxRequest())
                            Yii::app()->end();
                        else
                            $this->redirect(array('view', 'id' => $model->id));
                    }
                } catch (Exception $e) {
                    $transaction->rollback();
                    throw $e;
                }
            }
        }
    }

    public function actionCreate() {
        $model = new PageContent;
        $this->savePageContent($model);
        $this->render('create', array('model' => $model));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id, 'PageContent');
        $this->savePageContent($model);
        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->getRequest()->getIsPostRequest()) {
            $this->loadModel($id, 'PageContent')->delete();

            if (!Yii::app()->getRequest()->getIsAjaxRequest())
                $this->redirect(array('admin'));
        } else
            throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('PageContent');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new PageContent('search');
        $model->unsetAttributes();

        if (isset($_GET['PageContent']))
            $model->setAttributes($_GET['PageContent']);

        $this->render('admin', array(
            'model' => $model,
        ));
    }

}
